<?php
use_helper('I18N');
?>
<script type="text/javascript">
$(function() {
    $("#topupForm").validate({
        messages : {
            transactionPassword: {
                remote: "<?php echo __("Security Password is not valid")?>"
            }
        },
        rules : {

        },
        submitHandler: function(form) {
            var radioId = $('input:radio[name=radio_packageId]:checked').val();
            $("#list option[value='2']").text()
            //$('#formattedTextField').autoNumericSet($('#specialPackageId_' + packageId).val());
            //console.log("packageId", packageId);
            var packageId = $('#specialPackageId_' + radioId).val();
            var pointPackageDisplay = $('#specialPackageId_' + radioId + " option[value='" + packageId + "']").text();
            var pointPackageNeeded = $('#specialPackageId_' + radioId + " option[value='" + packageId + "']").autoNumericGet();
            var pointAvail = parseFloat($('#currentAccount').autoNumericGet());

            $("#packageId").val(packageId);

            var sure = confirm("<?php echo __('Are you sure want to purchase this package')?> " + pointPackageDisplay + "?");
            if (sure) {
                if (pointAvail == 0 || pointAvail == "" || parseFloat(pointAvail) < parseFloat(pointPackageNeeded)) {
                    error("<?php echo __("In-sufficient fund to purchase package.");?>");
                } else {
                    waiting();
                    form.submit();
                }
            }
        }
    });
    $(".packageSelect").change(function(event) {
        event.preventDefault();

        var packageNameArr = $(this).attr("id").split("_");
        var packageName = packageNameArr[1];
        $("#packageId_" + packageName).prop('checked', true);
    });
    $("#btnSubmit").click(function(event) {
        event.preventDefault();

        $("#topupForm").submit();
    });
});
</script>
<input type="hidden" id="formattedTextField">
<div class="row">
    <div class="col-md-12">
        <h2 class="page-title"><?php echo __("Purchase Package"); ?>
            <small></small>
        </h2>
    </div>
</div>
<div class="row">
    <div class="col-md-7">
        <section class="widget">
            <header>
                <h4>
                    <i class="icon-ok-sign"></i>
                    <?php echo __("Package Purchase"); ?>
                    <small></small>
                </h4>
            </header>
            <div class="body">
                <form class="form-horizontal label-left" method="post"
                      action="/member/purchasePackage"
                      data-validate="parsley"
                      id="topupForm" name="topupForm">

                    <input type="hidden" name="doAction" id="doAction" value="PURCHASE">
                    <input type="hidden" name="packageId" id="packageId">
                    <fieldset>
                        <legend class="section">
                        <?php echo __("Package Purchase")?>
                        </legend>
                        <?php include_component('component', 'alert', array('param' => $sf_user->getAttribute(Globals::SESSION_DISTID, 0))) ?>
                        <div class="row">
                            <div class="col-sm-8">
                                <div class="control-group">
                                    <label class="control-label" for="currentAccount">
                                    <?php echo __("Current Account")?>
                                    </label>

                                    <div class="controls form-group">
                                        <input type="text" readonly="readonly" id="currentAccount" name="currentAccount"  value="<?php echo number_format($pointAvailable, 2); ?>" class="form-control">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <table class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th align="center"><?php echo __('Join Package') ?></th>
                                        <th align="center"><?php echo __('Price') ?>(<?php echo $systemCurrency; ?>)</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                        $checkString = "checked='checked'";
                                        if (count($packageDBs) > 0) {
                                            $idx = 1;
                                            $packageName = "";
                                            $combo = "";
                                            foreach ($packageDBs as $packageDB) {
                                                if ($packageName != $packageDB->getPackageName()) {
                                                    if ($packageName != "") {
                                                        echo "<tr>
                                                            <td align='center'>" . $idx . "</td>
                                                            <td align='left'><label class='radio'>
                                                                <input class='iCheck' type='radio' id='packageId_".$idx."' value='".$idx."' name='radio_packageId' ".$checkString.">" . __($packageName) . "</label></td>
                                                            <td align='center'>" . $combo . "</td>
                                                        </tr>";
                                                        $checkString = "";
                                                        $idx++;
                                                    }
                                                    $combo = "<select style='width:150px; text-align:right' class='packageSelect' name='specialPackageId_".$idx."' id='specialPackageId_".$idx."'>";
                                                    $packagePrice = $packageDB->getPrice();
                                                    $combo .= "<option value='".$packageDB->getPackageId()."'>".number_format($packagePrice, 0)."</option>";
                                                } else {
                                                    $packagePrice = $packageDB->getPrice();
                                                    $combo .= "<option value='".$packageDB->getPackageId()."'>".number_format($packagePrice, 0)."</option>";
                                                    continue;
                                                }

                                                $packageName = $packageDB->getPackageName();
                                            }
                                            $combo .= "</select>";
                                            echo "<tr>
                                                <td align='center'>" . $idx . "</td>
                                                <td align='left'><label class='radio'>
                                                    <input class='iCheck' type='radio' class='packageSelect' id='packageId_".$idx."' value='".$idx."' name='radio_packageId' ".$checkString.">" . __($packageName) . "</label></td>
                                                <td align='center'>" . $combo . "</td>
                                            </tr>";
                                            $idx++;
                                        } else {
                                            echo "<tr class='odd' align='center'><td colspan='4'>" . __('No data available in table') . "</td></tr>";
                                        }
                                    ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </fieldset>
                    <div class="form-actions">
                        <button type="button" id="btnSubmit" class="btn btn-danger">
                            <i class="icon-ok bigger-110"></i>
                            <?php echo __("Submit");?>
                        </button>
                        <a href="/member/summary" class="btn btn-default"><?php echo __("Cancel");?></a>
                    </div>
                </form>
            </div>
        </section>
    </div>
</div>